import { useEffect, useRef, useState } from "react"
import HomePage_ModalComponent from "../../../../component/modal"
import HomePage_TableComponent from "../../../../component/table"
import { Divider, message } from "antd"
import HomePage_DataMaintenancePage_MaintenanceScheduledInspectionDataPage_AddOrEditModalComponent from "./component/addOrEditModal"
import HomePage_DataMaintenancePage_MaintenanceScheduledInspectionDataPage_SeeModalComponent from "./component/seeModal"
import { $eventBus, $EventBus_off, $EventBus_on, $export_excel } from "../../../../../../common/utils"
import { $Http } from "../../../../../../network"
import HomePage_ModalComponent_ConfirmationModallComponent from "../../../../component/modal/component/confirmationModal"
import dayjs from 'dayjs';

// 主页-数据维护页面-定检数据维护页面
const HomePage_DataMaintenancePage_MaintenanceScheduledInspectionDataPage = () => {
    let [pageNo, setPageNo] = useState<number>(1)
    let [pageSize, setPageSize] = useState<number>(10)
    const [sourceData, setSourceData] = useState<object>({})
    const [isDeleteModal, setIsDeleteModal] = useState<boolean>(false)
    const [modalData, setModalData] = useState<object>({})
    const [isSeeModal, setIsSeeModal] = useState<boolean>(false)
    const [isEditModal, setIsEditModal] = useState<boolean>(false)
    const columnList = [
        {
            title: '序号',
            width: 60,
            render: (text: string, record: object, idx: number) => idx + 1,
        },
        {
            title: '巡维中心',
            minWidth: 85,
            dataIndex: 'maintainCentre',
            render: (text: string) => text ?? "-"
        },
        {
            title: '安装站点',
            minWidth: 85,
            dataIndex: 'stationName',
            render: (text: string) => text ?? "-"
        },
        {
            title: '调度编号',
            minWidth: 85,
            dataIndex: 'dispatchNumber',
            align: "center",
            render: (text: string) => text ?? "-"
        },
        {
            title: '装置厂家',
            minWidth: 85,
            dataIndex: 'deviceFac',
            render: (text: string) => text ?? "-"
        },
        {
            title: '装置型号',
            minWidth: 85,
            dataIndex: 'deviceModel',
            render: (text: string) => text ?? "-"
        },
        {
            title: '定检周期',
            minWidth: 85,
            dataIndex: 'inspectionCycle',
            render: (text: string) => text ?? "-"
        },
        {
            title: '最近维护时间',
            minWidth: 120,
            dataIndex: 'maintainTime',
            render: (text: string) => text ?? "-"
        },
        {
            title: '维护班组',
            minWidth: 85,
            dataIndex: 'maintainTeam',
            render: (text: string) => text ?? "-"
        },
        {
            title: '维护责任人',
            minWidth: 100,
            dataIndex: 'maintainPerson',
            render: (text: string) => text ?? "-"
        },
        {
            title: '维护记录',
            minWidth: 85,
            dataIndex: 'maintainSituation',
            render: (text: string, record: object, idx: number) => <span className="homePage-tableOperationBtn" onClick={() => click_operation("see", record)}>查看</span>,
            align: "center"
        },
        {
            title: '操作',
            align: "right",
            width: 110,
            fixed: "right",
            render: (text: string, record: object, idx: number) => <>
                <span className="homePage-tableOperationBtn" onClick={() => click_operation("edit", record)}>编辑</span>
                <Divider className="homePage-tableOperationDivider" type="vertical" />
                <span className="homePage-tableOperationBtn" onClick={() => click_operation("delete", record)}>删除</span>
            </>
        },
    ];


    const http_search = () => {
        $Http("search", { typeName: "数据维护/定检数据维护",  pageNo, pageSize }).then((res) => {
            setSourceData({ ...res["data"] })
        })
    }

    const click_operation = (type: string, data: object) => {
        switch (type) {
            case "delete":
                setIsDeleteModal(true)
                setModalData(data)
                break
            default:
                if (data["workOrderID"]) {
                    $Http("search", { typeName: "系统设置/审核工单", id: data["workOrderID"], processResult: 0, pageNo: 1, pageSize: 1 }).then((res) => {
                        data = { ...data, ...res["data"]["list"][0] }
                        setIsSeeModal(true)
                        setModalData({ type, ...data })
                    })

                } else {
                    switch(type){
                        case "see" :
                            setIsSeeModal(true)
                            break                     
                        case "edit" :
                            setIsEditModal(true)
                            break
                    }
                    setModalData({ type, ...data })
                }

                break
        }
    }

    const change_pagination = (no: number, size: number) => {
        pageNo = no
        pageSize = size
        setPageNo(pageNo)
        setPageSize(pageSize)
        http_search()
    }
    const change_export = (list: Array<number>) => {
               $export_excel("数据维护/定检数据维护", list)
    }
    // 广播函数数据
    const eventBusFuncData = {
        // 头部操作新增
        headerOperationAdd: (data) => {
            setModalData({ type: "add" })
            setIsEditModal(true)
        },
        // 确认操作
        confirmOperation: (data: object) => {
            $Http("delete", { typeName: "数据维护/定检数据维护", id: data["data"]["id"] ,dispatchNumber: data["data"]["dispatchNumber"]}).then((res) => {
                message.success(res["success"])
                setTimeout(() => {
                    $eventBus.emit("headerOperationConditionSwitch", { source: "HomePage_DataMaintenancePage_SubstationMaintenancePage", explain: "删除成功，请关闭头部操作条件，并查询", data: { isOpen: false } })
                })
                setIsDeleteModal(false)
            })
        }
    }
    useEffect(() => {
        http_search()
        $EventBus_on(eventBusFuncData)
        return () => {
            $EventBus_off(eventBusFuncData)
        }
    }, [])
    return (
        <>
            <HomePage_TableComponent name="homePageDataMaintenancePageMainTransformerMaintenancePageTable"
                isRowSelection
                alignCenterList={["调度编号", "维护记录"]}
                alignRightList={["操作"]}
                columnList={columnList} pageNo={pageNo} pageSize={pageSize}  sourceData={sourceData} change_pagination={change_pagination} change_selectedRowList={change_export} />
            {
                isSeeModal && <HomePage_ModalComponent width={1000} change_cancel={setIsSeeModal} contentComponent={<HomePage_DataMaintenancePage_MaintenanceScheduledInspectionDataPage_SeeModalComponent modalData={modalData} change_cancel={setIsSeeModal} />} />
            }
            {
                isEditModal && <HomePage_ModalComponent width={640} change_cancel={setIsEditModal} contentComponent={<HomePage_DataMaintenancePage_MaintenanceScheduledInspectionDataPage_AddOrEditModalComponent sourceData={modalData} change_cancel={setIsEditModal} />} />
            }
            {
                isDeleteModal && <HomePage_ModalComponent width={500} change_cancel={setIsDeleteModal} contentComponent={<HomePage_ModalComponent_ConfirmationModallComponent title={`删除定检数据`} content={`确认删除 “${modalData["dispatchNumber"]}” 吗？`} operation="删除" sourceData={modalData} change_cancel={setIsDeleteModal} />} />
            }
        </>
    )
}
export default HomePage_DataMaintenancePage_MaintenanceScheduledInspectionDataPage